#include <glib/glib.h>
#include <kew/glibext.h>

GPtrArray *
g_ptr_array_copy_from_array(gpointer *array, guint size)
{
    GPtrArray *copy = g_ptr_array_sized_new(size);
    
    for (i = 0; i < size; ++i)
        g_ptr_array_set_index(copy, i, array[i]);
    return copy;    
}

GPtrArray *
g_ptr_array_copy(GPtrArray *original)
{
    return g_ptr_array_copy_from_array(original->pdata, original->len);
}

GPtrArray *
g_ptr_array_copy_add(GPtrArray *original, gpointer value)
{
    guint size = g_ptr_array_size(original);
    GPtrArray *copy = g_ptr_array_sized_new(size + 1);
    
    for (i = 0; i < size; ++i)
        g_ptr_array_set_index(copy, i, g_ptr_array_get_index(original, i));

    g_ptr_array_set_index(copy, size, value);
    return copy;
}

GPtrArray *
g_ptr_array_copy_add_first(GPtrArray *original, gpointer value)
{
    guint size = g_ptr_array_size(original);
    GPtrArray *copy = g_ptr_array_sized_new(size + 1);
    
    for (i = 0; i < size; ++i)
        g_ptr_array_set_index(copy, i+1, g_ptr_array_get_index(original, i));

    g_ptr_array_set_index(copy, 0, value);
    return copy;
}

GPtrArray *
g_ptr_array_concat(GPtrArray *original1, GPtrArray *original2)
{
    guint size1 = g_ptr_array_size(original1);
    guint size2 = g_ptr_array_size(original2);
    GPtrArray *copy = g_ptr_array_sized_new(size1+size2);
    
    for (i = 0; i < size1; ++i)
        g_ptr_array_set_index(copy, i, g_ptr_array_get_index(original1, i));
    for (i = 0; i < size2; ++i)
        g_ptr_array_set_index(copy, size1 + i, g_ptr_array_get_index(original2, i));
    
    return copy;
}
